//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj成员管理(搜索)

#import "SearchMembersVC.h"
#import "MJArcManageTbvCell.h"
#import "MJSearchBarView.h"
#import "MemberInfoVC.h"

@interface SearchMembersVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger pageNumber;
@property (weak, nonatomic) IBOutlet UIView *viewContent;
@property (weak, nonatomic) IBOutlet MJSearchBarView *searchBarView;
@property (weak, nonatomic) IBOutlet MJUITableView *tableView;
   
@end

@implementation SearchMembersVC

- (void)viewDidLoad {
    self.backgroundColorType = BackgroundColorTypeGray;
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)mj_setUI{
    [super mj_setUI];
    MJWeakSelf
    [self.navigationController setNavigationBarHidden:true animated:NO];
    self.pageNumber = 1;
    self.searchBarView.leftButton.hidden = false;
    [self.searchBarView.leftButton mj_addAction:^(UIButton *button) {
        [weakSelf.searchBarView.searchTextField resignFirstResponder];
        [weakSelf mj_popViewController];
    }];
    self.searchBarView.searchTextField.returnKeyType = UIReturnKeySearch; //设置按键类型
    self.searchBarView.searchTextField.delegate = self;
    [self.searchBarView.searchTextField becomeFirstResponder];
    
    
    [[self rac_signalForSelector:@selector(textFieldShouldReturn:) fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
        UITextField *tf = (UITextField *)x.first;
         [tf resignFirstResponder];
//         weakSelf.pageNumber = 1;
//         [weakSelf requestData];
//        [weakSelf searchPatient];
        
    }];
    [self.searchBarView.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    MJUITableView *tableView = self.tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 100;
    [MJArcManageTbvCell registerNibWithTableView:tableView];
//    tableView.mj_header = [MJUIRefreshHeader headerWithRefreshingBlock:^{
//       weakSelf.pageNumber = 1;
//       [weakSelf requestData];
//    }];
//    tableView.mj_footer = [MJUIRefreshFooter footerWithRefreshingBlock:^{
//       [weakSelf requestData];
//    }];
}

#pragma mark - 方法
- (void)textFieldDidChange:(UITextField *)textField{
    NSString *searchStr = self.searchBarView.searchTextField.text.length ? self.searchBarView.searchTextField.text : @"";
    if(searchStr.length){
        self.dataArray = [WCBDHelper mj_getPatientArrayWithSearchString:searchStr];
        [self showEmptyView];
        [self.tableView reloadData];
    }
}

- (void)requestData{
    
    NSString *searchStr = self.searchBarView.searchTextField.text.length ? self.searchBarView.searchTextField.text : @"";
    NSDictionary *parm = @{@"current":@(self.pageNumber),@"patientName":searchStr};
//    if(![searchStr mj_isChinese]){
//        parm = @{@"current":@(self.pageNumber),@"phone":searchStr};
//    }
    MJWeakSelf
    [MJHttpNetWorking requestWithParams:parm requestUri:patient_select requestType:RequestTypeGet showLoading:true success:^(MJBaseModel *model) {
        
        if (model.code ==  200){
            weakSelf.hideLoading = true;
            NSArray *data = model.data;
            NSMutableArray *modelArray = [NSMutableArray array];
            for (int i = 0; i<data.count; i++) {
                PatientModel *rmodel = [PatientModel yy_modelWithDictionary:data[i]];
                [modelArray addObject:rmodel];
            }
            [weakSelf processData:modelArray];
         }
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)processData:(NSArray *)modelArray{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    
    if(self.pageNumber == 1){
        [self.tableView.mj_footer resetNoMoreData];
        [self.dataArray removeAllObjects];
    }
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.dataArray addObjectsFromArray:modelArray];
    [self showEmptyView];
    //
    [self.tableView reloadData];
    self.pageNumber++;
}

- (void)showEmptyView{
    if(self.dataArray.count > 0){
        [self hideEmptyView:self.tableView];
        self.tableView.mj_footer.hidden = false;
    }else{
        self.tableView.mj_footer.hidden = true;
        [self showEmptyView:self.tableView emptyStyle:EmptyViewSearchArchive];
    }
}

#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MJWeakSelf
    MJArcManageTbvCell *cell = [MJArcManageTbvCell reuseForTableView:tableView forIndexPath:indexPath];
    PatientModel *patientModel =  self.dataArray[indexPath.row];
    cell.model = patientModel;
    [cell.detectionImageView mj_addTapActionWithBlock:^{
        [HealthHelper jumpDetectionViewControllerWith:patientModel viewController:weakSelf];
    }];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MemberInfoVC *vc = [[MemberInfoVC alloc]init];
    PatientModel *model = self.dataArray[indexPath.row];
    vc.patientModel = model;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
